﻿/**************************************************************
*作者：Leon
*创建时间：2022/4/5 22:14:19
**************************************************************/
using System.Windows;
using System.Windows.Input;

namespace Lad.DrawProject.Controls;

public static class SelectionProps
{

    #region EnabledForSelection

    public static readonly DependencyProperty EnabledForSelectionProperty =
        DependencyProperty.RegisterAttached("EnabledForSelection", typeof(bool), typeof(SelectionProps),
            new FrameworkPropertyMetadata((bool)false,
                new PropertyChangedCallback(OnEnabledForSelectionChanged)));

    public static bool GetEnabledForSelection(DependencyObject d)
    {
        return (bool)d.GetValue(EnabledForSelectionProperty);
    }

    public static void SetEnabledForSelection(DependencyObject d, bool value)
    {
        d.SetValue(EnabledForSelectionProperty, value);
    }

    private static void OnEnabledForSelectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement fe = (FrameworkElement)d;
        if ((bool)e.NewValue)
        {
            fe.PreviewMouseDown += Fe_PreviewMouseDown;
        }
        else
        {
            fe.PreviewMouseDown -= Fe_PreviewMouseDown;
        }
    }

    #endregion
    private static IBaseCanvasViewModel? serviceProvider;

    static void Fe_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        var selectableDesignerItemViewModelBase =
            (SelectableItemViewModelBase)((FrameworkElement)sender).DataContext;

        if (selectableDesignerItemViewModelBase != null && selectableDesignerItemViewModelBase.IsHitTestVisible)
        {
            if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
            {
                if ((Keyboard.Modifiers & (ModifierKeys.Shift)) != ModifierKeys.None)
                {
                    selectableDesignerItemViewModelBase.IsSelected = !selectableDesignerItemViewModelBase.IsSelected;
                }

                if ((Keyboard.Modifiers & (ModifierKeys.Control)) != ModifierKeys.None)
                {
                    selectableDesignerItemViewModelBase.IsSelected = !selectableDesignerItemViewModelBase.IsSelected;
                }
            }
            else if (!selectableDesignerItemViewModelBase.IsSelected)
            {
                
                if(serviceProvider == null)
                    serviceProvider = ((e.Source as DependencyObject).GetBaseCanvas())?.DataContext as ScrollViewCanvasViewModel;
                if (serviceProvider != null)
                {
                    foreach (var item in serviceProvider.SelectedItems)
                        item.IsSelected = false;

                    serviceProvider.SelectedItems.Clear();
                    //selectableDesignerItemViewModelBase.IsSelected = true;
                    serviceProvider.SelectionService.AddToSelection(selectableDesignerItemViewModelBase);
                }

            }
        }
    }
}
